package uiservices;
import io.appium.java_client.AppiumDriver;
import modules.FlyReaderModule;
import org.junit.Test;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import util.CommonUtil;

import java.util.ArrayList;
import java.util.List;

public class UiOperation {

    CommonUtil commonUtil = new CommonUtil();
    List<FlyReaderModule> flyReadAllinformation = new ArrayList<FlyReaderModule>();

    @Test
    public void homePage(){
        AppiumDriver driver;
        FlyReaderModule flyReaderModule = new FlyReaderModule();
        String deviceName = "ba5f906b";
        String platformVersion = "7.1.1";
        driver = commonUtil.startApp(deviceName,platformVersion);
        commonUtil.sleep(10000);
        //循环2次，性别分别为男，女
        for(int i=0;i<2;i++){
            //点击 我的
            driver.findElement(By.id("com.yuewen.cooperate.reader.free:id/radio_button_profile")).click();
            commonUtil.sleep(5000);
            //点击 我的偏好
            driver.findElement(By.id("com.yuewen.cooperate.reader.free:id/rl_mine_preference")).click();
            commonUtil.sleep(5000);
            if(i==0){
                //选择性别 男
                driver.findElement(By.name("男频")).click();
                flyReaderModule.setSex("男");
            }else{
                //选择性别 女
                driver.findElement(By.name("女频")).click();
                flyReaderModule.setSex("女");
            }
            commonUtil.sleep(8000);
            commonUtil.swipeToup(1);//滑动一次
            //点击 精选
            driver.findElement(By.id("com.yuewen.cooperate.reader.free:id/radio_button_recommend")).click();
            commonUtil.sleep(5000);
            String name="推荐";
            int channelNumber = 0;
            //获取 推荐频道
            if(channelNumber==0){
                driver.findElement(By.name("推荐")).click();
                commonUtil.swipLow();
                commonUtil.sleep(5000);
                modelDetail(flyReaderModule,driver,name,channelNumber);
                channelNumber=1;
            }
            if(channelNumber==1 && i==0){
                name="男生";
                driver.findElement(By.name("男生")).click();
                commonUtil.sleep(5000);
                commonUtil.swipLow();
                commonUtil.sleep(5000);
                modelDetail(flyReaderModule,driver,name,channelNumber);
                channelNumber=2;
            }
            if(channelNumber==2 && i==0){
                name="女生";
                driver.findElement(By.name("女生")).click();
                commonUtil.sleep(5000);
                commonUtil.swipLow();
                commonUtil.sleep(5000);
                modelDetail(flyReaderModule,driver,name,channelNumber);
            }
            //写入数据到CSV
            commonUtil.csvWrite(flyReadAllinformation);
        }
    }


    /**
     *  进入详情页，获取书籍信息
     * @param bookList
     * @param flyReaderModule
     */
    public boolean detail(List<WebElement> bookList, FlyReaderModule flyReaderModule,AppiumDriver driver){
        int allsize = 0;
        for(int i =0;i < bookList.size();i++){
            FlyReaderModule flyReaderModuleAll= new FlyReaderModule();
            WebElement book =bookList.get(i);
            book.click();
            //获取 书籍名称
            commonUtil.sleep(5000);
            WebElement bookElement = driver.findElement(By.id("com.yuewen.cooperate.reader.free:id/tv_title"));
            String bookName = bookElement.getText();
            boolean isEquales = commonUtil.distance(bookName,flyReadAllinformation);
            if(!isEquales){
                allsize++;
                driver.findElement(By.id("com.yuewen.cooperate.reader.free:id/profile_header_left_button")).click();
                if(allsize == bookList.size()){
                    return false;
                }
            }else{
                flyReaderModuleAll.setBookName(bookName);
                //获取 作者
                WebElement authorElement = driver.findElement(By.id("com.yuewen.cooperate.reader.free:id/tv_line1_info2"));
                flyReaderModuleAll.setAuthorName(authorElement.getText());
                //获取 版权  页面滑动4次
                commonUtil.swipeToup(4);
                By id = By.id("com.yuewen.cooperate.reader.free:id/tv_origin_copyright");
                boolean idExist= commonUtil.byElementIsExist(id);
                if(!idExist){
                    WebElement copyrightElement = driver.findElement(id);
                    flyReaderModuleAll.setCopyright(copyrightElement.getText());
                }else {
                    flyReaderModuleAll.setCopyright("本书由飞读进行电子版制作与发行");
                }
                commonUtil.sleep(5000);
                //退出详情页
                driver.findElement(By.id("com.yuewen.cooperate.reader.free:id/profile_header_left_button")).click();
                flyReaderModuleAll.setModelName(flyReaderModule.getModelName());
                flyReaderModuleAll.setSex(flyReaderModule.getSex());
                flyReaderModuleAll.setChannel(flyReaderModule.getChannel());
                flyReadAllinformation.add(flyReaderModuleAll);
            }
        }
        return true;
    }

    /**
     *  不同的频道书籍详情公共方法
     * @param flyReaderModule
     * @param driver
     * @param name
     * @param channelNumber
     */
    public void modelDetail(FlyReaderModule flyReaderModule,AppiumDriver driver,String name,int channelNumber){
        //flyReaderModule.setChannel(commonUtil.channelName(name,channelNumber));
        flyReaderModule.setChannel(name);
        //点击 更多
        driver.findElement(By.id("com.yuewen.cooperate.reader.free:id/tv_subtitle_more")).click();
        commonUtil.sleep(5000);
        //获取  书籍所在专题
        WebElement modelNameElement = driver.findElement(By.id("com.yuewen.cooperate.reader.free:id/profile_header_title"));
        flyReaderModule.setModelName(modelNameElement.getText());
        commonUtil.sleep(5000);
        //点击 书籍详情页
        boolean isEnd=true;
        while (isEnd){
           // By endid=By.id("com.yuewen.cooperate.reader.free:id/loadingText");
          //  isEnd=commonUtil.byElementIsExist(endid);
            List<WebElement> bookList = driver.findElements(By.id("com.yuewen.cooperate.reader.free:id/iv_cover"));
            isEnd = detail(bookList,flyReaderModule,driver);
            commonUtil.sleep(5000);
            commonUtil.swipeToup(2);
//            if(isEnd){
//
//            }
        }
        commonUtil.sleep(5000);
        driver.findElement(By.id("com.yuewen.cooperate.reader.free:id/profile_header_left_button")).click();
    }
}
